package com.neusoft.ribbon.controller;


import com.neusoft.common.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
public class UserController {

    //	private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://EUREKA-CLIENT-USER";

    /**
     * 使用 使用restTemplate访问restful接口非常的简单。
     */
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient ;

    @RequestMapping("/queryStu.do")
    public Student findStudent()
    {
        return restTemplate.getForObject(REST_URL_PREFIX+"/findStudent.do", Student.class);
    }

    @RequestMapping("/queryStuByid/{id}")
    public Student findStudent0001(@PathVariable("id") Integer id)
    {
        return restTemplate.getForObject(REST_URL_PREFIX+"/findStu.do/"+id,Student.class);
    }


    // 测试@EnableDiscoveryClient,消费端可以调用服务发现
    @RequestMapping(value = "/discovery.do")
    public Object discovery()
    {
        return restTemplate.getForObject(REST_URL_PREFIX + "/user/discovery", Object.class);
    }



    @RequestMapping(value = "/consumer/lbtest", method = RequestMethod.GET)
    public String test(){
        String serviceId = "EUREKA-CLIENT-USER";
        String result = "";
        for (int i = 0; i < 15; i++) {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose(serviceId);
            result += "第"+( i + 1) + "次：" + serviceInstance.getHost() + ": " + serviceInstance.getPort()+"<br>";
        }
        return result;
    }
}
